package com.hnxxxy.workshop.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hnxxxy.workshop.mapper.WorkerMapper;
import com.hnxxxy.workshop.model.DepotDO;
import com.hnxxxy.workshop.model.WorkerDO;
import com.hnxxxy.workshop.request.WorkerPageRequest;
import com.hnxxxy.workshop.request.WorkerRequest;
import com.hnxxxy.workshop.request.WorkshopRequest;
import com.hnxxxy.workshop.service.WorkerService;
import com.hnxxxy.workshop.vo.DepotVO;
import com.hnxxxy.workshop.vo.WorkerVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 工人 服务实现类
 * </p>
 *
 * @author zqh
 * @since 2022-05-09
 */
@Service
@Slf4j
public class WorkerServiceImpl implements WorkerService {

    @Resource
    private WorkerMapper workerMapper;

    @Override
    public void add(WorkerRequest workerRequest) {
        WorkerDO workerDO = new WorkerDO();
        BeanUtils.copyProperties(workerRequest, workerDO);

        int rows = workerMapper.insert(workerDO);
        log.info("新增消息:rows={},data={}",rows,workerDO);

    }

    @Override
    public void deleteById(int id) {
        int rows = workerMapper.deleteById(id);
        log.info("删除消息条数 :rows={}",rows);

    }

    @Override
    public void update(WorkerRequest workerRequest) {
        WorkerDO workerDO = new WorkerDO();
        BeanUtils.copyProperties(workerRequest, workerDO);

        int rows = workerMapper.updateById(workerDO);
        log.info("修改消息:rows={},data={}",rows,workerDO);
    }

    @Override
    public WorkerVO findByIdWorker(int id) {
        WorkerVO workerVO = new WorkerVO();

        WorkerDO workerDO = workerMapper.selectById(id);
        BeanUtils.copyProperties(workerDO, workerVO);
        log.info("根据id查询信息:id={}  depotVO={}",id,workerVO);
        return workerVO;
    }

    @Override
    public Map<String, Object> page(WorkerPageRequest workerPageRequest) {
        int page = workerPageRequest.getPage();
        int size = workerPageRequest.getSize();

        String name = workerPageRequest.getName();
        String type = workerPageRequest.getType();

        QueryWrapper<WorkerDO> queryWrapper = new QueryWrapper<WorkerDO>();

        if (!StrUtil.hasEmpty(name)){
            queryWrapper.like("name", name);
        }
        if (!StrUtil.hasEmpty(type)){
            queryWrapper.like("type", type);
        }

        Page<WorkerDO> pageInfo = new Page<>(page, size);

        IPage<WorkerDO> workerDOIPage = workerMapper.selectPage(pageInfo,queryWrapper);

        Map<String,Object> pageMap = new HashMap<>(3);

        pageMap.put("total_record", workerDOIPage.getTotal());
        pageMap.put("total_page", workerDOIPage.getPages());
        pageMap.put("current_data",workerDOIPage.getRecords()
                .stream().map(obj->beanProocess(obj)).collect(Collectors.toList()));

        log.info("分页消息:pageMap={}",pageMap);
        return pageMap;

    }

    private WorkerVO beanProocess(WorkerDO obj) {
        WorkerVO workerVO = new WorkerVO();
        BeanUtils.copyProperties(obj, workerVO);
        return workerVO;
    }
}
